<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
      <title>Tagged with motion sensors - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=motion+sensors</link>
      <pubDate>Sun, 08 Aug 2021 18:38:15 +0000</pubDate>
         <description>Tagged with motion sensors - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/taggedmotion+sensors/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>To create a real time graph</title>
      <link>https://forum.processing.org/two/discussion/24117/to-create-a-real-time-graph</link>
      <pubDate>Thu, 14 Sep 2017 06:48:21 +0000</pubDate>
      <dc:creator>Nitin1992</dc:creator>
      <guid isPermaLink="false">24117@/two/discussions</guid>
      <description><![CDATA[<p>Hello all,</p>

<p>I am writing a graphical representation of movement/rotations of a motion sensor. I am able to write the code to print the values and graphical representation of rotation of sensor. But now i am stuck with plotting the graph for the same w.r.t the values real time. Following is the code,</p>

<pre><code>   //graphics object we will use to buffer our drawing
    PGraphics graphics;
    ToxiclibsSupport gfx;
    float[] q = new float[4];
    Quaternion quat = new Quaternion(1, 0, 0, 0);
    Quaternion quat1 = new Quaternion(1, 0, 0, radians(90));
    //window dimensions
    //int multiplicator = 100;
    //int window_x = 16*multiplicator;
    //int window_y = 9*multiplicator;
    int window_x = 1024;
    int window_y = 768;

    int lastCallLogic = 0; //absolute time when logic thread was called
    int lastCallRender = 0; //lastCallRender
    int lastCallMisc = 0;

    //time passed since last call 
    int deltaTLogic = 0;
    int deltaTRender = 0;

    //how often we already called the threads
    int countLogicCalls = 0;
    int countRenderCalls = 0;

    //used to know  how many calls since last fps calculation
    int countLogicCallsOld = 0;
    int countRenderCallsOld = 0;

        String inBuffer1,inBuffer2;

    //framerate of Logic/Render threads
    //-1 to run as fast as  possible. be prepared to melt your pc!
    int framerateLogic = 500;
    int framerateRender = 200;

    int framerateMisc = 1; //how often the framerate display will be updated
    int no_of_sensor = 2;
    Serial[] ports = new Serial[2];    // the serial ports
    float[] yaw = new float[no_of_sensor];
    float[] pitch = new float[no_of_sensor];
    float[] roll = new float[no_of_sensor];
    PFont f;
    void setup() {

      //init window
      //size(window_x, window_y); //creates a new window
      size(1024, 768, OPENGL); //creates a new window
      gfx = new ToxiclibsSupport(this);
       f = createFont("Arial",20,true);
      graphics = createGraphics(window_x, window_y);//creates the draw area
      frameRate(framerateRender); //tells the draw function to run
        ports[0] = new Serial(this, "COM6"/**Serial.list()[0]*/, 230400);  // upper arm : we will assume as of now
        ports[0].clear();
       textFont(f);
       textAlign(LEFT);
       fill(255,0,0);
      logicThread.start();

      println(Thread.currentThread().getName() +" : the MainThread is running and used to Render");
    }

    //draw function. This is our Render Thread
    void draw() {

      countRenderCalls++;

      graphics.beginDraw();

      //CODE TO DRAW GOES HERE
       background(0);
           text(roll[0],10,50);
        text(pitch[0],10,90);
        text(yaw[0],10,130);
        text(q[0],10,170);
        text(q[1],10,210);
        text(q[2],10,250);
        text(q[3],10,290);
      pushMatrix();
        translate(1024/4, 768/2, -50);
        scale(4,4,4);

       rotateZ(radians(pitch[0]));
        rotateX(radians(yaw[0]));
        rotateY(radians(roll[0]));
        buildBoxShape();

      popMatrix();
        pushMatrix();
        translate(3*(1024/4), 768/2, -50);
        scale(4,4,4);

        float[] axis = quat.toAxisAngle();
        rotate(axis[0], -axis[1], axis[3], axis[2]);
        //buildBoxShape();
        buildBoxShape();
      popMatrix();


      //-------------
      graphics.endDraw();
      image(graphics, 0, 0);
    }

    void buildBoxShape() {
      //box(60, 10, 40);
      noStroke();
      beginShape(QUADS);

      //Z+ (to the drawing area)
      fill(#00ff00);
      vertex(-30, -5, 20);
      vertex(30, -5, 20);
      vertex(30, 5, 20);
      vertex(-30, 5, 20);

      //Z-
      fill(#0000ff);
      vertex(-30, -5, -20);
      vertex(30, -5, -20);
      vertex(30, 5, -20);
      vertex(-30, 5, -20);

      //X-
      fill(#ff0000);
      vertex(-30, -5, -20);
      vertex(-30, -5, 20);
      vertex(-30, 5, 20);
      vertex(-30, 5, -20);

      //X+
      fill(#ffff00);
      vertex(30, -5, -20);
      vertex(30, -5, 20);
      vertex(30, 5, 20);
      vertex(30, 5, -20);

      //Y-
      fill(#ff00ff);
      vertex(-30, -5, -20);
      vertex(30, -5, -20);
      vertex(30, -5, 20);
      vertex(-30, -5, 20);

      //Y+
      fill(#00ffff);
      vertex(-30, 5, -20);
      vertex(30, 5, -20);
      vertex(30, 5, 20);
      vertex(-30, 5, 20);

      endShape();
    }
    Thread logicThread = new Thread(new Runnable() {
      public void run() {
        System.out.println(Thread.currentThread().getName() + " : the logicThread is running");

        //main Logic loop
        while (true) {


          countLogicCalls++;
      // Sensor no : 0
      if (ports[0].available() &gt; 6) {
      inBuffer1 = ports[0].readStringUntil('\r');
      }

            if (inBuffer1 != null){
            convert_raw_data_to_angles(inBuffer1,0); 
            convert_raw_data_to_quaternions(inBuffer1);
          }

          //------------
          //framelimiter
          int timeToWait = 1000/framerateLogic - (millis()-lastCallLogic); // set framerateLogic to -1 to not limit;
          if (timeToWait &gt; 1) {
            try {
              //sleep long enough so we aren't faster than the logicFPS
              Thread.currentThread().sleep( timeToWait );
            }
            catch ( InterruptedException e )
            {
              e.printStackTrace();
              Thread.currentThread().interrupt();
            }
          }
          /**
          example why we wait excactly: 1000/framerate - (millis-lastcall)

           framerate = 100 //framerate we want
           1000/framerate = 10 //time for one loop
           millis = 1952 //current time
           last call logic = 1949 //time when last logic loop finished

           how  long should the programm wait??

           millis-lastcall = 3 -&gt; the whole loop took 3ms

           1000/framerate - (millis-lastcall) = 7ms -&gt; we will have to wait 7ms to keep a framerate of 100

           */

          //remember when the last logic loop finished
          lastCallLogic = millis();


          //End of main logic loop
        }
      }
    }
    );


    void convert_raw_data_to_angles(String inBuffer, int i)
    {
        int[] num = int(split(inBuffer,' '));
        if(num.length &gt;= 9){
        // Heading(Yaw) angle
        yaw[i] = float(((num[3])&lt;&lt;8 | num[2])/16);    
        roll[i] = float(((num[5])&lt;&lt;8 | num[4])/16);
        pitch[i] = float(((num[7])&lt;&lt;8 | num[6])/16);
        if(pitch[i] &gt; 180.0F)
          pitch[i] = pitch[i] - 4096;
        if(roll[i] &gt; 90.0F)
          roll[i] = roll[i] - 4096;
       }
    }
    void convert_raw_data_to_quaternions(String inBuffer)
    {
       int[] num = int(split(inBuffer,' '));
        if(num.length &gt;= 9){
        // Heading(Yaw) angle
        q[0] = ((num[9])&lt;&lt;8 | num[8])/16384.000000F;    
        q[1] = ((num[11])&lt;&lt;8 | num[10])/16384.000000F;
        q[2] = ((num[13])&lt;&lt;8 | num[12])/16384.000000F;
        q[3] = ((num[15])&lt;&lt;8 | num[14])/16384.000000F;
        for (int i = 0; i &lt; 4; i++) if (q[i] &gt;= 2) q[i] = -4 + q[i];
       }
       // set our toxilibs quaternion to new data
       quat.set(q[0], q[1], q[2], q[3]);
    }
</code></pre>

<p>I have to plot the graph real time w.r.t to any two values/all. Please help me out in solving this.</p>

<p>Thanks in advance</p>
]]></description>
   </item>
   <item>
      <title>Controlling Arduino with Processing through Bluetooth</title>
      <link>https://forum.processing.org/two/discussion/14789/controlling-arduino-with-processing-through-bluetooth</link>
      <pubDate>Sat, 06 Feb 2016 18:37:57 +0000</pubDate>
      <dc:creator>CharlesDesign</dc:creator>
      <guid isPermaLink="false">14789@/two/discussions</guid>
      <description><![CDATA[<p>Hi all,</p>

<p>I'm looking to use an evolutionary algorithm to control a small robot built with Arduino.</p>

<p>I need to get feedback from the sensors attached to Arduino as fast as possible into Processing.
And once processed give some instructions back to the Arduino.</p>

<p>Is it possible to control Arduino from processing wirelessly?</p>

<p>Thanks,
Charles</p>
]]></description>
   </item>
   <item>
      <title>How to Manipulate Video Playback Speed using FrameDifferencing?</title>
      <link>https://forum.processing.org/two/discussion/13746/how-to-manipulate-video-playback-speed-using-framedifferencing</link>
      <pubDate>Wed, 02 Dec 2015 16:34:43 +0000</pubDate>
      <dc:creator>bkeum</dc:creator>
      <guid isPermaLink="false">13746@/two/discussions</guid>
      <description><![CDATA[<p>Hi!</p>

<p>I'm trying to combine the FrameDifferencing library example, which quantifies the amount of movement in a video frame of the webcam, and using numbers in movementSum to manipulate the speed of a video that I import into the program. (Example: if the video that I wanted to play was a scene from the Matrix, I would want it so that when I run the program, the more intense the movement in front of the webcam, the faster the speed of the playback in the Matrix video.) Right now, I'm having trouble figuring out</p>

<p>1) how to get the data from the movement of the webcam (so, turning on the webcam and using that as in input) but not drawing it. (What I want to draw is the Matrix video—where do I put the information for that?)
2) where and/how to use the movementSum numbers, which are integers, to correspond to the video playback speed of the Matrix.</p>

<p>Below is the code for the FrameDifferencing example by Golan Levin:</p>

<p>/**
 * Frame Differencing 
 * by Golan Levin. 
 *
 * Quantify the amount of movement in the video frame using frame-differencing.
 */</p>

<p>import processing.video.*;</p>

<p>int numPixels;
int[] previousFrame;
Capture video;</p>

<p>void setup() {
  size(640, 480);</p>

<p>// This the default video input, see the GettingStartedCapture 
  // example if it creates an error
  video = new Capture(this, width, height);</p>

<p>// Start capturing the images from the camera
  video.start();</p>

<p>numPixels = video.width * video.height;
  // Create an array to store the previously captured frame
  previousFrame = new int[numPixels];
  loadPixels();
}</p>

<p>void draw() {
  if (video.available()) {
    // When using video to manipulate the screen, use video.available() and
    // video.read() inside the draw() method so that it's safe to draw to the screen
    video.read(); // Read the new frame from the camera
    video.loadPixels(); // Make its pixels[] array available</p>

<pre><code>int movementSum = 0; // Amount of movement in the frame
for (int i = 0; i &lt; numPixels; i++) { // For each pixel in the video frame...
  color currColor = video.pixels[i];
  color prevColor = previousFrame[i];
  // Extract the red, green, and blue components from current pixel
  int currR = (currColor &gt;&gt; 16) &amp; 0xFF; // Like red(), but faster
  int currG = (currColor &gt;&gt; 8) &amp; 0xFF;
  int currB = currColor &amp; 0xFF;
  // Extract red, green, and blue components from previous pixel
  int prevR = (prevColor &gt;&gt; 16) &amp; 0xFF;
  int prevG = (prevColor &gt;&gt; 8) &amp; 0xFF;
  int prevB = prevColor &amp; 0xFF;
  // Compute the difference of the red, green, and blue values
  int diffR = abs(currR - prevR);
  int diffG = abs(currG - prevG);
  int diffB = abs(currB - prevB);
  // Add these differences to the running tally
  movementSum += diffR + diffG + diffB;
  // Render the difference image to the screen
  pixels[i] = color(diffR, diffG, diffB);
  // The following line is much faster, but more confusing to read
  //pixels[i] = 0xff000000 | (diffR &lt;&lt; 16) | (diffG &lt;&lt; 8) | diffB;
  // Save the current color into the 'previous' buffer
  previousFrame[i] = currColor;
}
// To prevent flicker from frames that are all black (no movement),
// only update the screen if the image has changed.
if (movementSum &gt; 0) {
  updatePixels();
  println(movementSum); // Print the total amount of movement to the console
}
</code></pre>

<p>}
}</p>
]]></description>
   </item>
   <item>
      <title>Motion sensor signal using</title>
      <link>https://forum.processing.org/two/discussion/11936/motion-sensor-signal-using</link>
      <pubDate>Sun, 02 Aug 2015 13:21:35 +0000</pubDate>
      <dc:creator>juozap</dc:creator>
      <guid isPermaLink="false">11936@/two/discussions</guid>
      <description><![CDATA[<p>Hey guys, I'm fairly new to processing and coding in general, the reason I came to it was to find out how could I make certain video and/or audio to change playing speed/frame rate accordingly to motion sensor data. I understand that those kind of tasks are not for beginners to make happen so I thought maybe some more experienced people might give some ideas/advice how it could be done (if possible at all).
Cheers!</p>
]]></description>
   </item>
   <item>
      <title>Video, motion sensors, and arduino</title>
      <link>https://forum.processing.org/two/discussion/11415/video-motion-sensors-and-arduino</link>
      <pubDate>Tue, 23 Jun 2015 14:59:49 +0000</pubDate>
      <dc:creator>kinesthtiaSonja</dc:creator>
      <guid isPermaLink="false">11415@/two/discussions</guid>
      <description><![CDATA[<p>I am trying to create a project that uses processing and Arduino with motion sensors. Basically I am trying to set up video that fades in and out or cross fades in relation to people's movement in a room. Does anyone know how to do this? i am relatively new to processing and still trying to figure stuff out!</p>
]]></description>
   </item>
   </channel>
</rss>